package com.community.process._1010.dao.entity;

import com.baomidou.mybatisplus.annotation.TableName;
import com.community.process._1010.common.base.BaseEntity;
import com.community.process._1010.dao.vo.ProcessingCouponTypeVo;
import com.community.process._1010.domains.ProcessOrderDTO;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDateTime;

/**
 * 到餐加工子订单商品列表
 *
 * @author Zbs
 * @since 2024-09-05
 */
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@TableName("yg_process_order_item")
@Schema(description = "到餐加工子订单商品列表")
public class ProcessOrderItem extends BaseEntity implements Serializable {
    private static final long serialVersionUID = 1L;

    @Schema(description = "子订单ID")
    private Long orderId;

    @Schema(description = "商品ID")
    private Long itemId;

    @Schema(description = "商品类型 1 自带食材 2 成品菜 3 自组食材")
    private Integer type;

    @Schema(description = "商品名称")
    private String itemName;

    @Schema(description = "商品数量")
    private Integer itemNum;

    @Schema(description = "商品单价")
    private Long itemPrice;

    @Schema(description = "商品总价")
    private Long totalPrice;

    @Schema(description = "单位")
    private String units;

    @Schema(description = "单位数量")
    private Integer unitsNum;

    @Schema(description = "图片")
    private String image;

    public ProcessOrderItem(ProcessOrderDTO.Cart cart, ProcessOrderDTO.ItemCart itemCart, ProcessIngredients ingredients) {
        this.itemId = itemCart.getItemId();
        this.type = cart.getType();
        this.itemName = ingredients.getName();
        this.itemNum = itemCart.getItemNum();
        this.units = ingredients.getUnits();
        this.image = ingredients.getImage();
        this.unitsNum = ingredients.getUnitsNum();
        if (cart.getType().equals(1)) {
            // 自带食材
            this.itemPrice = ingredients.getProcessingFee();
        } else if (cart.getType().equals(3)){
            // 自组食材
            this.itemPrice = ingredients.getPrice();
        }
        this.totalPrice = new BigDecimal(itemCart.getItemNum())
                .divide(new BigDecimal(ingredients.getUnitsNum()),0, RoundingMode.HALF_UP)
                .multiply(new BigDecimal(this.itemPrice)).longValue();
    }

    public ProcessOrderItem(ProcessOrderDTO.Cart cart, ProcessOrderDTO.ItemCart itemCart, ProcessingCouponTypeVo ingredients) {
        this.itemId = itemCart.getItemId();
        this.type = cart.getType();
        this.itemName = ingredients.getName();
        this.itemNum = itemCart.getItemNum();
        this.itemPrice = ingredients.getAmount();
        //商品数量*商品单价
        this.totalPrice =new BigDecimal(itemCart.getItemNum())
                .multiply(new BigDecimal(this.itemPrice)).longValue();
    }

    public ProcessOrderItem(ProcessOrderDTO.Cart cart, ProcessOrderDTO.ItemCart itemCart,ProcessFoodItem foodItem) {
        this.itemId = itemCart.getItemId();
        this.type = cart.getType();
        this.itemName = foodItem.getName();
        this.itemNum = itemCart.getItemNum();
        this.itemPrice = foodItem.getPrice();
        this.units = foodItem.getUnits();
        this.image = foodItem.getImage();
        this.totalPrice = new BigDecimal(itemCart.getItemNum()).multiply(new BigDecimal(foodItem.getPrice())).longValue();
        this.unitsNum = 1;
    }
}
